﻿using Models.Mailbox.Mail;
using Models.Mailbox;
using Newtonsoft.Json;
using SendMessagePage.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace SendMessagePage.HomePage
{
    /// <summary>
    /// SetTaskPage.xaml 的交互逻辑
    /// </summary>
    public partial class SetTaskPage : Window
    {
        public SetTaskPage()
        {
            InitializeComponent();
            Closing += Window_Closing;
        }
        //查询定时任务
        private string SelectMailboxConfigure = ConfigurationManager.AppSettings["SelectMailboxConfigure"];

        private ObservableCollection<TaskClass> taskList = new ObservableCollection<TaskClass>();

        private async void Select_Click(object sender, RoutedEventArgs e)
        {
            string taskName = this.txtTaskName.Text;

            if (!string.IsNullOrEmpty(taskName)) {

                SelectMailboxConfigure += "TaskName=" + taskName;
            }


            string result = await AccessWebApiUtil.PostAsyncJson(SelectMailboxConfigure, "", PackingUserLogin.token);

            OrderResult<MailboxTaskConfig> orderResult = JsonConvert.DeserializeObject<OrderResult<MailboxTaskConfig>>(result);

            taskList.Clear();
            if (orderResult.result != null && orderResult.result.Count > 0)
            {

                foreach (var item in orderResult.result)
                {
                    TaskClass task = new TaskClass()
                    {
                        Id = item.Id,
                        TaskName = item.TaskName,
                        TaskRemark = item.TaskRemark,
                        TaskState = item.TaskState == 0 ? "开启" : "关闭",
                        TaskTime = item.TaskTime.ToString("yyyy-MM-dd")


                    };


                    taskList.Add(task);
                }
            }
            dgvOrderList.ItemsSource = taskList;
        }


        private async void SetTask_Click(object sender, RoutedEventArgs e)
        {
            var SelectedRow = (TaskClass)dgvOrderList.SelectedItem;

            AddRegularTaskPage taskPage = new AddRegularTaskPage(SelectedRow);
            taskPage.Show();
            this.Close();

            MessageBox.Show("");
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();
            if (MessageBox.Show("是否要关闭？", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                e.Cancel = false;
                mainWindow.Show();


            }
            else
            {
                e.Cancel = true;
            }



        }

    }
}
